R: printing a plot and a table together
What do you wanna do?:
I wanna put the plot and table together.
This uses some technique covered in R: printing multiple plots side-by-side
R package used:
#gridExtra
#ggplot2
The code is partially based on the following tutorial
https://www.r-bloggers.com/plotting-tables-alsongside-charts-in-r/
code::R
`{r mtld boxplot}
tidy_mtld <- augment(mtld_3)
plot_MTLD <- ggplot(tidy_mtld, aes(x = as.factor(Level), y = .fitted, color = as.factor(Level))) +
geom_boxplot(notch = T, outlier.alpha = 0) +
geom_jitter(alpha = .2) +
coord_flip() +
labs(x = "Graded Reader Levels (1 - 6)", y = "Measure of Lexical Textual Diversity") +
theme(legend.position = "none")
`
`{r}
q <- c(0.25, 0.5, 0.75)
MTLD_table <- data.frame(
"Level1" = tapply(tidy_mtld$.fitted, Level, quantile, c(0.25, .50, .75))1,
"Level2" = tapply(tidy_mtld$.fitted, Level, quantile, c(0.25, .50, .75))2,
"Level3" = tapply(tidy_mtld$.fitted, Level, quantile, c(0.25, .50, .75))3,
"Level4" = tapply(tidy_mtld$.fitted, Level, quantile, c(0.25, .50, .75))4,
"Level5" = tapply(tidy_mtld$.fitted, Level, quantile, c(0.25, .50, .75))5,
"Level6" = tapply(tidy_mtld$.fitted, Level, quantile, c(0.25, .50, .75))6
)
names(MTLD_table) <- c("Level1", "Level2", "Level3", "Level4", "Level5", "Level6")
# Set theme to allow for plotmath expressions
tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)))
tbl_MTLD <- tableGrob(data.frame(round(MTLD_table,3)), theme=tt)
grid.arrange(plot_MTLD, tbl_MTLD,
nrow=2,
as.table=TRUE,
heights = c(5,2))
`